package service

import (
	"ksd-social-api/commons/base/service"
	"ksd-social-api/commons/page"
	"ksd-social-api/modules/user/model"
	context2 "ksd-social-api/modules/user/model/context"
	vo2 "ksd-social-api/modules/user/model/vo"
	"ksd-social-api/utils"
)

type UserWalletService struct {
	service.BaseService
}

/**
 * 充值订单保存
 * @author feige
 * @date 2023-12-18
 * @version 1.0
 * @desc
 */
func (service *UserWalletService) SaveUserWalletRecords(
	OutTradeNo string, TransactionId string, orderJson string, payMethod int, payMethodName string, price string,
	userId uint64, systemId uint64, systemName string) bool {
	// 这里就是解析下单的附属参数
	// 根据课程id查询课程信息
	userVo := userMapper.GetUserByID(userId, systemId)
	var userWalletRecords model.UserWalletRecords
	userWalletRecords.SystemId = systemId
	userWalletRecords.SystemName = systemName
	// 处理返回结果
	userWalletRecords.Title = "学习币充值"
	userWalletRecords.Price = price
	userWalletRecords.UserId = userId
	userWalletRecords.Username = userVo.Username
	userWalletRecords.Avatar = userVo.Avatar
	userWalletRecords.Uuid = userVo.Uuid
	userWalletRecords.Phone = userVo.Telephone
	userWalletRecords.Address = userVo.Address
	userWalletRecords.Nickname = userVo.Nickname

	userWalletRecords.PayMethod = payMethod
	userWalletRecords.PayMethodName = payMethodName
	userWalletRecords.Tradeno = OutTradeNo
	userWalletRecords.Orderno = TransactionId
	userWalletRecords.OrderJson = orderJson
	userWalletRecords.Price = price

	// 保存订单的接口
	return userWalletMapper.SaveUserWalletRecords(userWalletRecords)
}

/**
 * 统计订单是否支付--充值
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *UserWalletService) CountUserWalletRecordsByOutTradeNo(OutTradeNo string, systemId uint64) int {
	return userWalletMapper.CountUserWalletRecordsByOutTradeNo(OutTradeNo, systemId)
}

/**
 * @desc 查询文章列表信息
 * @author feige
 * @date 2023-11-14
 * @version 1.0
 */
func (service *UserWalletService) FindUserWalletRecords(ctx context2.UserWalletContext) *page.Page {
	userWalletRecprdsPage, _ := userWalletMapper.FindUserWalletRecordsPage(ctx.UserId, ctx.SystemId, ctx.PageNo, ctx.PageSize)
	userWalletRecordsList := userWalletRecprdsPage.Records.([]model.UserWalletRecords)
	if userWalletRecordsList != nil {
		userWalletRecordsVos := []vo2.UserWalletRecordsVo{}
		for _, topics := range userWalletRecordsList {
			userWalletRecordsVo := vo2.UserWalletRecordsVo{}
			utils.CopyProperties(&userWalletRecordsVo, topics)
			userWalletRecordsVos = append(userWalletRecordsVos, userWalletRecordsVo)
		}
		userWalletRecprdsPage.Records = userWalletRecordsVos
	}
	return userWalletRecprdsPage
}
